package es.xpt.ysf.commons.codes;

import java.sql.ResultSet;
import java.sql.SQLException;

import es.xpt.ysf.commons.codes.model.Code;
import es.xpt.ysf.dao.Connection;
import es.xpt.ysf.dao.DAOException;
import es.xpt.ysf.dao.DbDAO;
import es.xpt.ysf.dao.PreparedStatement;
import es.xpt.ysf.jaas.YSFPrincipal;

public class CodesDAO extends DbDAO<Code> {
	private static final String CODE_FIELDS = " COD.code COD_code, COD.company COD_company,COD.period COD_period,"
			+ " COD.value COD_value, COD.pattern COD_pattern, COD.initialValue COD_initialValue, COD.incValue COD_incValue,"
			+ " COD.modify COD_modify";

	public CodesDAO(Connection c, YSFPrincipal principal) {
		super(c, principal);
	}


	@Override
	protected Code populateBean(ResultSet rs) throws SQLException {

		Code code = new Code();
		code.setCode(rs.getString("COD_code"));
		code.setCompany(rs.getString("COD_company"));
		code.setPeriod(rs.getString("COD_period"));
		code.setValue(rs.getLong("COD_value"));
		code.setPattern(rs.getString("COD_pattern"));
		code.setInitialValue(rs.getInt("COD_initialValue"));
		code.setIncValue(rs.getInt("COD_incValue"));
		code.setModify(rs.getString("COD_modify"));
		return code;

	}

	public Code getCode(String code, String company, String period)
			throws DAOException {
		String selectStatement = "select " + CODE_FIELDS 
				+ " from ysf_cc_code COD" + " where COD.code = '" + code
				+ "' and COD.company = '" + company + "' and "
				+ " COD.period = '" + period + "' ";

		return singleQuery(selectStatement);
	}
	
	public Code findByName (String code, String company) throws DAOException {
		String selectStatement = "select " + CODE_FIELDS 
			+ " from ysf_cc_code COD" + " where COD.code = '" + code
			+ "' and COD.company = '" + company + "'";

		return singleQuery(selectStatement);
	}

	public void updateCode(Code code) throws DAOException {
		String statement = " update ysf_cc_code set value  = ?"
				+ " where code = ? and company = ? and period = ? ";

		PreparedStatement stmt = null;
		try {
			stmt = con.prepareStatement(statement);
			stmt.setLong(1, code.getValue());
			stmt.setString(2, code.getCode());
			stmt.setString(3, code.getCompany());
			stmt.setString(4, code.getPeriod());

			stmt.executeUpdate();

		} catch (SQLException se) {
			throw new DAOException(se, errors);
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	public void insertCode(Code code) throws DAOException {
		String statement = "insert into ysf_cc_code "
				+ " (code, company, period, value, pattern, initialValue, incValue, modify)"
				+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement stmt = null;
		try {
			stmt = con.prepareStatement(statement);
			stmt.setString(1, code.getCode());
			stmt.setString(2, code.getCompany());
			stmt.setString(3, code.getPeriod());
			stmt.setLong(4, code.getValue());
			stmt.setString(5, code.getPattern());
			stmt.setLong(6, code.getInitialValue());
			stmt.setLong(7, code.getIncValue());
			stmt.setString(8, code.getModify());

			stmt.executeUpdate();

		} catch (SQLException se) {
			throw new DAOException(se, errors);
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}
}